package com.ymttest.business.service.productdata.service;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymatou.iapi.productquery.parameter.ProductDetail.ProductInDetailBean;
import com.ymatou.iapi.productquery.service.GetProductInfoByProductIdCall;
import com.ymatou.iapi.selleractivity.service.ApplyActivityCall;
import com.ymatou.iapi.sellerproduct.service.AddNewSellerProductCall;
import com.ymttest.business.service.AdminCallService;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.utils.FileUtil;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.compare.AssertHelper;
import com.ymttest.utils.compare.LogPathConf;
import com.ymttest.utils.compare.LoggerHelper;
import com.ymttest.utils.compare.LoggerInterface;

public class StageActivityProduct {
	
	public static void main(String args[]){
		//System.out.println(args[0]);
		
		
		//每个买手商品数量
		int perSellerProdCount = 10;
		//活动ID
		int activityId = 7519;
		
		createActivityProduct(perSellerProdCount,activityId);
		
	}
	
	
	
	
	public static void createActivityProduct(int perSellerProdCount,int activityId){
		LogPathConf logPathConf = new LogPathConf(false);
		LoggerInterface logger = new LoggerHelper(logPathConf);
		AssertHelper.setLoggerInterface(logger);
		
		
		String userIdAndName = System.getProperty("user.dir") + File.separator + "resource" + File.separator
				+"CompareData"+File.separator+"userId_userName.txt"; 
		List<String> userIdAndNameList = FileUtil.readFileAsLine(userIdAndName);
		
		String jsonFile = "com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl_ForStageTest.json";
		JSONObject addProductBean = JsonBeanHelper.getJsonBean(jsonFile);
				
		
		for(int k=0;k<userIdAndNameList.size();k++){
			String[] userIdAndNameAry =  userIdAndNameList.get(k).split(",");
			int sellerId = Integer.parseInt(userIdAndNameAry[0]);
			JsonBeanHelper.updateJsonBean(addProductBean, MapUtil.hashMap("UserID", sellerId,"LoginID",userIdAndNameAry[1]));
			
			List<String> products = new ArrayList<>();
			for (int j=0;j<perSellerProdCount;j++){
				try {
					products.add(new AddNewSellerProductCall().callAndGetReturnDataAndMgoNoCheck(addProductBean).getJSONObject("Data").getString("ProductID"));
				} catch (JSONException e) {
					logger.failure("", e);
				}
			}
			
			YMTDateUtil.waitTime(5);
			for (int j=0;j<perSellerProdCount;j++){
				try {
					String productId = products.get(j);
					JSONObject prodDetail = new GetProductInfoByProductIdCall().callAndGetReturnData(productId);
					
					ProductInDetailBean prodBean = ProductQueryCallService.getBeanFromJson(prodDetail.getJSONObject("Data").getJSONObject("Product"), ProductInDetailBean.class);
					List<String> catalogList = prodBean.getCatalogList().stream().map(x->x.getCatalogId()).collect(Collectors.toList());
					
					// updateMap do nothing
					ApplyActivityCall activityCall = new ApplyActivityCall();
					JSONObject applyEffectiveActBean = activityCall.getJsonBean(null);

					String applyMsg = "";
					Map<String, Object> requestMap = new HashMap<String, Object>();
					requestMap.put("SellerId", sellerId);
					requestMap.put("ActivityId", activityId);
					requestMap.put("ActivityProducts[0]->ProductId", productId);
					requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->CatalogId", catalogList.get(0));
					requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->ActivityStockNum", 100000);
					requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->ActivityPrice", 10);
					requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->Sku", null);
					requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->BkcKey", null);
					requestMap.put("ActivityProducts[0]->ApplyCatalogs[0]->ProductInActivityId", 0);

					for(int i=1;i<catalogList.size();i++){
						JSONArray CatalogListBean = applyEffectiveActBean.getJSONArray("ActivityProducts").getJSONObject(0).getJSONArray("ApplyCatalogs");
						CatalogListBean.put(i, new JSONObject(CatalogListBean.get(0).toString()));
						CatalogListBean.getJSONObject(i).put("CatalogId",catalogList.get(i));
						CatalogListBean.getJSONObject(i).put("ActivityStockNum",100000);
						CatalogListBean.getJSONObject(i).put("ActivityPrice",10);
						CatalogListBean.getJSONObject(i).put("Sku","");
						CatalogListBean.getJSONObject(i).put("BkcKey","");
						CatalogListBean.getJSONObject(i).put("ProductInActivityId",0);
					}
					
					
					JsonBeanHelper.updateJsonBean(applyEffectiveActBean, requestMap);

					activityCall.setData(applyEffectiveActBean);
					activityCall.callService();
					JSONObject ret = new JSONObject(activityCall.getReturnData());
					applyMsg = activityCall.getReturnData();
					if (ret.has("Data") && ret.getJSONObject("Data").has("ApplySuccessProducts")
							&& ret.getJSONObject("Data").getJSONArray("ApplySuccessProducts").length() > 0) {
						
						String inaid = ret.getJSONObject("Data").getJSONArray("ApplySuccessProducts").getJSONObject(0)
								.getString("ProductInActivityId");
						AdminCallService.approveProductInActivity("fengyifeng@ymatou.com", "1qaz2wsx1", inaid, MapUtil
								.hashMap("productBeginTime", YMTDateUtil.getBeforeOrNextSecond(300), "productEndTime", "2019-8-3 15:08:50"));
						
						String msgSuccess = "product: " + productId +  ",inaid:" + inaid;
						System.out.println(msgSuccess);
						logger.verifyEquals(true, true, msgSuccess);
						
					} else {
						logger.failure(applyMsg);
					}
					
					
					catalogList.forEach(ele->{
						String filePath = logPathConf.getFolderPath()+ File.separator+"ret.txt";
						FileUtil.writeAsLine(filePath, sellerId+","+ele);
					});
					
					
				} catch (Exception e) {
					logger.failure("", e);
				}
			}
			
			
			
		}
		
	}
}
